package com.th.supcom.pds.sortingdrug.client.widget;

import java.util.Arrays;
import java.util.List;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.th.supcom.pds.config.client.model.PhamWindowConfigModel;
import com.th.supcom.pds.sortingdrug.client.model.PdsSupplyProModel;

/**
 * @author 杨冲
 * 
 *         后台分拣处方信息面板
 */
public class SortingDrugDetailPanel extends ContentPanel
{

    private Button m_windowBtn;
    private PhamWindowConfigModel m_phamWindowConfig;
    private Grid <PdsSupplyProModel> m_grid;
    private ListStore <PdsSupplyProModel> m_store;
    private SortingDrugMainPanel m_mainPanel;
    private final static int size = 130;

    public SortingDrugDetailPanel (PhamWindowConfigModel phamWindowConfig)
    {
        m_phamWindowConfig = phamWindowConfig;
        init ();
    }

    public void setMainPanel (SortingDrugMainPanel mainPanel)
    {
        m_mainPanel = mainPanel;
    }

    /**
     * 初始化
     */
    private void init ()
    {
        setPixelSize (SortingDrugDetailPanel.size, 300);
        setHeaderVisible (false);
        setBodyBorder (false);
        setBorders (false);
        setLayout (new FlowLayout ());
        m_windowBtn = new Button ("<span style=\"font-weight:bold; font-size:20px;\">"
                                  + m_phamWindowConfig.getWindowName () + "号窗口</span>");
        m_windowBtn.setWidth (SortingDrugDetailPanel.size);
        m_windowBtn.setHeight (30);

        m_store = new ListStore <PdsSupplyProModel> ();

        ColumnConfig name = new ColumnConfig ("prescNo", "", SortingDrugDetailPanel.size);
        ColumnModel cm = new ColumnModel (Arrays.asList (name));

        LayoutContainer container = new LayoutContainer ();
        container.setHeight (270);
        container.setLayout (new FitLayout ());
        m_grid = new Grid <PdsSupplyProModel> (m_store, cm);
        m_grid.setHideHeaders (true);
        m_grid.setBorders (true);

        container.add (m_grid);
        add (m_windowBtn);
        add (container);

        addListeners ();
    }

    /**
     * 获取窗口名称
     * 
     * @return
     */
    public Long getWindowId ()
    {
        return m_phamWindowConfig.getPhamWindowConfigId ();
    }

    /**
     * 添加按钮事件
     */
    private void addListeners ()
    {

        m_windowBtn.addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (ButtonEvent ce)
            {
                SortingRecordWindow window = new SortingRecordWindow (m_store.getModels ());
                window.setMainPanel (m_mainPanel);
                window.show ();
            }
        });
    }

    /**
     * 初始化数据
     * 
     * @param list
     */
    public void initData (List <PdsSupplyProModel> list)
    {
        m_store.removeAll ();
        m_store.add (list);
    }

}
